
public InitializeDB()
{
	new String:error[255];
	new Handle:kv = CreateKeyValues("");
	KvSetString(kv, "driver", "sqlite");
	KvSetString(kv, "database", "PowerupsXDatabase");
	
	db = SQL_ConnectCustom(kv, error, sizeof(error), false);
	if(db == INVALID_HANDLE)
	{
		PrintToServer("Database Failiure: %s", error);
	}
	CloseHandle(kv);
	
	SQL_LockDatabase(db);
	SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS PowerupsXData (steam_id TEXT, ability TEXT, level INTEGER, experience INTEGER);");
	SQL_UnlockDatabase(db);
}

public UpdateData(Client)
{
	decl String:steamId[20];
	decl String:sAbility[32];
	decl String:buffer[200];
	
	if(Client && IsClientConnected(Client) && !IsFakeClient(Client))
	{
		GetClientAuthString(Client, steamId, sizeof(steamId));
		new Ability = PX_GetAbility(Client);
		PX_GetRegisteredCodename(Ability,sAbility);
		new Level = PX_GetLevel(Client,Ability);
		
		Format(buffer, sizeof(buffer), "UPDATE PowerupsXData SET level = '%d', experience = '%d' WHERE steam_id = '%s' AND ability = '%s';", Level, AbilityKTNL[Client], steamId, sAbility);
		SQL_TQuery(db, SQLErrorCheckCallback, buffer);
		LogMessage("Data Updated! Ability: %s. Level: %d. XP: %d", sAbility, Level, AbilityKTNL[Client]);
	}
}
public SQLErrorCheckCallback(Handle:owner, Handle:hndl, const String:error[], any:data)
{
	if(!StrEqual("", error))
	{
		PrintToServer("Data Update Error: %s", error);
	}
}

public LoadSavedData(Client)
{	
	if (IsFakeClient(Client))
		return;

	decl String:steamId[20];
	decl String:sAbility[32];
	
	GetClientAuthString(Client, steamId, sizeof(steamId));
	PX_GetRegisteredCodename(PX_GetAbility(Client),sAbility);
	
	new String:SQLQuery[512];

	Format(SQLQuery, sizeof(SQLQuery), "SELECT * FROM PowerupsXData WHERE steam_id = '%s' AND ability = '%s';", steamId, sAbility);
	SQL_TQuery(db, T_LoadSavedData, SQLQuery, Client);
}

public T_LoadSavedData(Handle:owner, Handle:hndl, const String:error[], any:Client)
{
	if(SQL_FetchRow(hndl))
	{
		new Level = GetSQLDataInt(hndl,"level");
		new XP = GetSQLDataInt(hndl,"experience");
		PX_SetLevel(Client, PX_GetAbility(Client), Level);
		AbilityKTNL[Client] = XP;
		LogMessage("Success: %d %d %d", PX_GetAbility(Client), Level, XP);
	}
	else
	{
		PX_SetLevel(Client, PX_GetAbility(Client), 0);
		AbilityKTNL[Client] = 0;
		LogError("Database Read Failiure!");
		InsertPlayer(Client);
	}
}

InsertPlayer(Client)
{
	decl String:steamId[20];
	decl String:sAbility[32];
	decl String:buffer[200];
	
	GetClientAuthString(Client, steamId, sizeof(steamId));
	new Ability = PX_GetAbility(Client);
	PX_GetRegisteredCodename(Ability,sAbility);
	new Level = PX_GetLevel(Client,Ability);
		
	Format(buffer, sizeof(buffer), "INSERT INTO PowerupsXData (steam_id,ability,level,experience) VALUES ('%s', '%s', '%i', '%i')", steamId, sAbility, Level, AbilityKTNL[Client]);
	SQL_TQuery(db, SQLErrorCheckCallback, buffer);
}

public GetSavedMenu(Client, Ability)
{	
	if (IsFakeClient(Client))
		return;

	decl String:steamId[20];
	decl String:sAbility[32];
	
	GetClientAuthString(Client, steamId, sizeof(steamId));
	PX_GetRegisteredCodename(Ability,sAbility);
	

	new String:SQLQuery[512];

	Format(SQLQuery, sizeof(SQLQuery), "SELECT * FROM PowerupsXData WHERE steam_id = '%s' AND ability = '%s';", steamId, sAbility);

	SQL_TQuery(db, T_GetSavedMenu, SQLQuery, Client);
}

public T_GetSavedMenu(Handle:owner, Handle:hndl, const String:error[], any:Client)
{
	if(SQL_FetchRow(hndl))
	{
		ClientMenuLevel[Client] = GetSQLDataInt(hndl,"level");
	}
	else
	{
		LogError("Database Read Failiure! Menu");
		ClientMenuLevel[Client] = 0;
	}
}

GetSQLDataInt(Handle:query,const String:columnname[])
{
    new column;
    SQL_FieldNameToNum(query,columnname,column);
    return SQL_FetchInt(query,column);
}

public Action:Command_Reset(Client, args)
{
	return Plugin_Handled;
}